﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using System.Collections;

namespace PServiceBus.Core.Manager {
    public class ObjectManagerBase<TObject> : IObjectManager<TObject> where TObject : class, IIdentifiable {

        public IObjectProvider<TObject> Provider { get; set; }

        #region IObjectManager<TObject> Members

        public bool Contains(TObject obj) {
            return Provider.Exists(obj);
        }

        public TObject Get(Guid id) {
            return Provider.Get(id);
        }

        public void Attach(TObject obj) {
            Provider.Add(obj);
        }

        public void Refresh(TObject obj) {
            Provider.Refresh(obj);
        }

        public void Detach(TObject obj) {
            Provider.Delete(obj);
        }

        public void AttachMany(IEnumerable<TObject> objs) {
            Provider.Add(objs);
        }

        public void DetachMany(IEnumerable<TObject> objs) {
            Provider.Delete(objs);
        }

        public void Clear() {
            Provider.Clear();
        }

        public void Delete() {
            Provider.Delete();
        }

        #endregion

        #region IEnumerable<TObject> Members

        public IEnumerator<TObject> GetEnumerator() {
            foreach (var obj in Provider) yield return obj;
        }

        #endregion

        #region IEnumerable Members

        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }

        #endregion
    }
}
